+angular.module('imsApp').controller('campaignsController', ['$scope', '$rootScope', '$state', 'CampaignsManager','$sce', function ($scope, $rootScope, $state, CampaignsManager,$sce) {
    $scope.$sce=$sce;
    $scope.search = {
        pageNo:$state.params.pageNo || 1,
        pageSize:$state.params.pageSize||10,
        keyword:$state.params.keyword,
        payType:$state.params.payType,
        status:$state.params.status
    };
    $scope.keyword=$scope.search.keyword;
    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name,$scope.search);
        }
    };

    $scope.searchChange=function () {
        $scope.search.pageNo=1;
        $state.go($state.current.name,$scope.search);
    };
    $scope.searchClick=function(keyword){
        $scope.search.keyword=keyword;
        $scope.search.pageNo=1;
        $state.go($state.current.name,$scope.search);

    }
    CampaignsManager.campaignsList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
        $scope.campaignsList = result.data;
        $scope.paginationConf.totalItems = result.data.total;

    })
    $scope.payType = {30:$scope.table[30],31:$scope.table[31], 32: $scope.table[32]};
    $scope.status = {6: $scope.table[6], 7: $scope.table[7], 8: $scope.table[8], 3: $scope.table[3]};
    $scope.deletes = function (key) {
          $scope.search[key]=null;
        $scope.search.pageNo=1;
        $state.go($state.current.name,$scope.search);
    }

    $scope.changeStatus=function(item){
        var status="";
        if(item.status==3){
            status=4;
        }else{
            status=3;
        }
        CampaignsManager.campaignsStatusEdit(item.campaignId,{status:status}).then(function(result){
            $state.reload();
        })
    }
}])

